package com.company.calendar.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.company.calendar.entity.Holiday;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 节假日Mapper接口
 * 
 * @author System
 * @since 1.0.0
 */
@Mapper
public interface HolidayMapper extends BaseMapper<Holiday> {
    
    /**
     * 根据公司ID和日期查找节假日
     */
    @Select("SELECT * FROM holidays WHERE company_id = #{companyId} AND holiday_date = #{holidayDate} AND is_deleted = 0")
    Holiday findByCompanyIdAndHolidayDateAndIsDeletedFalse(@Param("companyId") Long companyId, 
                                                          @Param("holidayDate") LocalDate holidayDate);
    
    /**
     * 根据公司ID查找指定日期范围的节假日
     */
    @Select("SELECT * FROM holidays WHERE company_id = #{companyId} " +
            "AND holiday_date BETWEEN #{startDate} AND #{endDate} " +
            "AND is_deleted = 0 ORDER BY holiday_date")
    List<Holiday> findByCompanyIdAndDateRange(@Param("companyId") Long companyId,
                                              @Param("startDate") LocalDate startDate,
                                              @Param("endDate") LocalDate endDate);
    
    /**
     * 根据公司ID查找所有节假日
     */
    @Select("SELECT * FROM holidays WHERE company_id = #{companyId} AND is_deleted = 0 ORDER BY holiday_date")
    List<Holiday> findByCompanyIdAndIsDeletedFalseOrderByHolidayDate(@Param("companyId") Long companyId);
    
    /**
     * 根据公司ID和年份查找节假日
     */
    @Select("SELECT * FROM holidays WHERE company_id = #{companyId} " +
            "AND YEAR(holiday_date) = #{year} " +
            "AND is_deleted = 0 ORDER BY holiday_date")
    List<Holiday> findByCompanyIdAndYear(@Param("companyId") Long companyId, @Param("year") Integer year);
    
    /**
     * 检查指定日期是否存在节假日
     */
    @Select("SELECT COUNT(*) FROM holidays WHERE company_id = #{companyId} AND holiday_date = #{holidayDate} AND is_deleted = 0")
    int existsByCompanyIdAndHolidayDateAndIsDeletedFalse(@Param("companyId") Long companyId, 
                                                        @Param("holidayDate") LocalDate holidayDate);
}
